package com.movisol.c2dm.helpers;

import java.util.Date;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

public class C2DMHelper {

	private final static String	KEY									= "c2dmPref";
	private final static String	REG_KEY								= "registrationKey";
	private static final String	REG_DATE_KEY						= "registrationKeySaveDate";
	private static final long	regIdTimeOut						= 172800000; //2 DAYS TIME OUT
	public static final String	EXTRA_SENDER						= "sender";
	public static final String	EXTRA_APPLICATION_PENDING_INTENT	= "app";
	public static final String	REQUEST_UNREGISTRATION_INTENT		= "com.google.android.c2dm.intent.UNREGISTER";
	public static final String	REQUEST_REGISTRATION_INTENT			= "com.google.android.c2dm.intent.REGISTER";
	public static final String	LAST_REGISTRATION_CHANGE			= "last_registration_change";
	public static final String	BACKOFF								= "backoff";
	private static final long	DEFAULT_BACKOFF						= 5000;
	
	
	//Restrict the instantiation of the class
	private C2DMHelper()
	{}

	/**
	 * Initiate c2d messaging registration for the current application
	 */
	public static void registerPushNotifications(Context context, String sender) {
		Log.w("C2DM", "Start registration process");
		Intent intent = new Intent(REQUEST_REGISTRATION_INTENT);
		intent.putExtra("app", PendingIntent.getBroadcast(context, 0, new Intent(), 0));
		// Sender currently not used
		intent.putExtra("sender", sender);
		context.startService(intent);
	}

	/**
	 * Unregister the application. New messages will be blocked by server.
	 */
	public static void unregisterPushNotifications(Context context) {
		Intent regIntent = new Intent(REQUEST_UNREGISTRATION_INTENT);
		regIntent.putExtra(EXTRA_APPLICATION_PENDING_INTENT,
				PendingIntent.getBroadcast(context, 0, new Intent(), 0));
		context.startService(regIntent);
	}

	public static long getLastRegistrationChange(Context context) {
		final SharedPreferences prefs = context.getSharedPreferences(KEY, Context.MODE_PRIVATE);
		return prefs.getLong(LAST_REGISTRATION_CHANGE, 0);
	}

	public static long getBackoff(Context context) {
		final SharedPreferences prefs = context.getSharedPreferences(KEY, Context.MODE_PRIVATE);
		return prefs.getLong(BACKOFF, DEFAULT_BACKOFF);
	}

	public static void setBackoff(Context context, long backoff) {
		final SharedPreferences prefs = context.getSharedPreferences(KEY, Context.MODE_PRIVATE);
		Editor editor = prefs.edit();
		editor.putLong(BACKOFF, backoff);
		editor.commit();
	}

	public static void clearRegistrationId(Context context) {
		final SharedPreferences prefs = context.getSharedPreferences(KEY, Context.MODE_PRIVATE);
		Editor editor = prefs.edit();
		editor.putString(REG_KEY, "");
		editor.putLong(LAST_REGISTRATION_CHANGE, System.currentTimeMillis());
		editor.commit();
	}

	public static void setRegistrationId(Context context, String registrationId) {
		final SharedPreferences prefs = context.getSharedPreferences(KEY, Context.MODE_PRIVATE);
		Editor editor = prefs.edit();
		editor.putString(REG_KEY, registrationId);
		Date now = new Date();
		editor.putLong(REG_DATE_KEY, now.getTime());
		editor.commit();
	}

	/**
	 * Return the current registration id. If result is empty, the registration
	 * has failed.
	 * 
	 * @return registration id, or empty string if the registration is not
	 *         complete.
	 */
	public static String getRegistrationId(Context context) {
		String registrationId = null;
		final SharedPreferences settings = context.getSharedPreferences(KEY, Context.MODE_PRIVATE);
		registrationId = settings.getString(REG_KEY, null);
		return registrationId;
	}

	public static boolean isRegistrationIdExpired(Context context) {
		long dateSaved = 0;
		final SharedPreferences settings = context.getSharedPreferences(KEY, Context.MODE_PRIVATE);
		dateSaved = settings.getLong(REG_DATE_KEY, 0);
		
		Date now = new Date();
		if(dateSaved + regIdTimeOut < now.getTime())
			return true;
		
		return false;
	}
}
